let requestTimes = 0;
const baseUrl = "https://temp.api.siruish.cn/api"
export const request = (params) => {
    requestTimes++;
    // 准备请求头
    let header = { ...params.header }
    if (params.url.includes("auth/")) {
        let token = wx.getStorageSync('access_token')
        if (token) {
            let tokenTime = wx.getStorageSync("tokenTime");
            let newTime = Date.parse(new Date());// 当前时间
            if (newTime - tokenTime > 7000000) {
                return new Promise((resolve, reject) => {
                    wx.showLoading({
                        title: "加载中",
                        mask: true,
                    });
                    wx.request({
                        url: baseUrl + '/v1/auth/refresh/token',
                        method: 'post',
                        header: {
                            token: token,
                            type: 'c_applet'
                        },
                        data: {
                            refresh_token: wx.getStorageSync('refresh_token')
                        },
                        success: (result) => {
                            if (result.data.errCode == 0) {
                                var access_token = result.data.data.token
                                var refresh_token = result.data.data.refresh_token
                                wx.setStorageSync("access_token", access_token);
                                wx.setStorageSync("refresh_token", refresh_token);
                                wx.setStorageSync('tokenTime', Date.parse(new Date()))
                                console.log(header, 'a');
                                header["token"] = access_token
                                header['type'] = 'c_applet'


                                wx.request({
                                    ...params,
                                    header: header,
                                    url: baseUrl + params.url,
                                    success: (res) => {
                                        if (res.data.errCode == 4001) {
                                            wx.showToast({
                                                title: res.data.message,
                                                duration: 1500,
                                                icon: 'none',
                                            })
                                            wx.navigateTo({
                                                url: '/pages/login/login'
                                            })
                                        } else {
                                            resolve(res)
                                        }
                                    },
                                    fail: (err) => {
                                        reject(err)
                                    }
                                   
                                })
                            } else {
                                wx.showToast({
                                    title: "登录失效，请重新登录",
                                    duration: 1500,
                                    icon: 'none',
                                })
                                // header["token"] = token
                                // header['type'] = 'c_applet'
                                setTimeout(() => {
                                    wx.navigateTo({
                                        url: '/pages/login/login'
                                    })
                                }, 1500)

                            }
                        },
                        fail: (err) => {
                            wx.showToast({
                                title: '请先登录',
                                duration: 1500,
                                icon: 'none',
                            })
                            setTimeout(() => {
                                wx.navigateTo({
                                    url: '/pages/login/login'
                                })
                            }, 1500)
                        },
                        complete: () => {
                            requestTimes--;
                            // 关闭正在加载的图标
                            if (requestTimes === 0) {
                                wx.hideLoading();
                            }
                        }

                    })
                })

            } else {
                header["token"] = token
                header['type'] = 'c_applet'
                wx.showLoading({
                    title: "加载中",
                    mask: true,
                });
                // 配置基准路径
                return new Promise((resolve, reject) => {
                    wx.request({
                        ...params,
                        header: header,
                        url: baseUrl + params.url,
                        success: (result) => {
                            if (result.data.errCode == 4001|| result.data.errCode == 2002 ) {
                                wx.showToast({
                                    title: result.data.message,
                                    duration: 1500,
                                    icon: 'none',
                                })
                                setTimeout(() => {
                                    wx.reLaunch({
                                        url: '/pages/login/login'
                                    })
                                }, 1500)
                            } else {
                                resolve(result)
                            }
                        },
                        fail: (err) => {
                            reject(err)
                        },
                        complete: () => {
                            requestTimes--;
                            // 关闭正在加载的图标
                            if (requestTimes === 0) {
                                wx.hideLoading();
                            }
                        }
                    })
                })
            }

        } else {
            wx.showToast({
                title: '请先登录',
                duration: 1500,
                icon: 'none',
            })
            requestTimes--;
            setTimeout(() => {
                wx.reLaunch({
                    url: '/pages/login/login'
                })
            }, 1500)
            return new Promise((resolve, reject) => {
                resolve()
            })
        }
    } else {
        wx.showLoading({
            title: "加载中",
            mask: true,
        });

        return new Promise((resolve, reject) => {
            wx.request({
                ...params,
                header: header,
                url: baseUrl + params.url,
                success: (result) => {
                    resolve(result)
                },
                fail: (err) => {
                    reject(err)
                },
                complete: () => {
                    requestTimes--;

                    // 关闭正在加载的图标
                    if (requestTimes === 0) {
                        wx.hideLoading();
                    }
                }
            })
        })
    }
}